const { getCourseData, changeField, changeCourseStatus } = require('../services/Course'),
      { getRecomCourseData, changeRecomCourseStatus } = require('../services/RecomCourse'),
      { getSliderData, changeSliderStatus } = require('../services/Slider'),
      { getCourseFieldData } = require('../services/CourseTab'),
      { getCollectionData, changeCollectionStatus } = require('../services/Collection'),
      { getTeacherData, changeTeacherStatus, selectStarTeacher } = require('../services/Teacher'),
      { getStudentData, changeStudentStatus } = require('../services/Student'),
      { API } = require('../config/error_config'),
      { returnInfo } = require('../libs/utils');

class Index {
    //项目初期为了设置session用的，现在貌似是没用了，（不确定）
    async index (ctx, next) {
        const sess = ctx.session;//因为我们koa框架中有了相关配置，我们直接这样就能访问到session

        if (!sess.uid) {
            sess.uid = 1;
            sess.username = 'jsjiajia';
            sess.nickname = 'js++';
            sess.gender = 'male';
        }

        ctx.body = {
            session: sess
        }
        // await ctx.render('index');
    }

    //获取课程数据和课程分类
    async getCourses (ctx, next) {
        const courseData = await getCourseData(),
              fieldData = await getCourseFieldData();

        if (courseData && fieldData) {
            ctx.body = returnInfo(API.RETURN_SUCCESS, {
                courseData,
                fieldData
            })
            
        } else {
            ctx.body = returnInfo(API.RETURN_FAILED);
        }
    }

    //改变课程分类
    async changeCourseField (ctx, next) {
        const { cid, field } = ctx.request.body;

        //result是操作数据库数据后，数据受影响的行数
        const result = await changeField(cid, field);

        if (!result) {
            ctx.body = returnInfo(API.CHANGE_COURSE_FIELD_FAILED);
            return;
        } 

        ctx.body = returnInfo(API.CHANGE_COURSE_FIELD_SUCCESS);
    }

    //获取推荐课程数据
    async getRecomCourse (ctx, next) {
        const data = await getRecomCourseData();

        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS, data)
                 : returnInfo(API.RETURN_FAILED);
    }

    //改变所有产品的上下架状态
    async changeDataStatus (ctx, next) {
        const { id, status, field } = ctx.request.body;
        let result = null;

        switch (field) {
            case 'COURSE':
                result = await changeCourseStatus(id, status);
                break;
            case 'RECOM_COURSE':
                result = await changeRecomCourseStatus(id, status);
                break;
            case 'SLIDER': 
                result = await changeSliderStatus(id, status);
                break;
            case 'COLLECTION': 
                result = await changeCollectionStatus(id, status);
                break;
            case 'TEACHER': 
                result = await changeTeacherStatus(id, status);
                break;
            case 'STUDENT': 
                result = await changeStudentStatus(id, status);
                break;
            default:
                ctx.body = returnInfo(API.FIELD_ERROR);
                return;
                break;
        }

        //这个result得到的是数据库数据受影响的行数
        //如果result是0，那就表示没有修改成功
        if (!result) {
            ctx.body = returnInfo(API.CHANGE_COURSE_STATUS_FAILED);
            return;
        }

        ctx.body = returnInfo(API.CHANGE_COURSE_STATUS_SUCCESS);
    }

    //获取轮播图数据
    async getSliders (ctx, next) {
        const data = await getSliderData();

        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS, data)
                 : returnInfo(API.RETURN_FAILED);
    }

    //获取'课程集合管理'的数据
    async getCollections (ctx, text) {
        const data = await getCollectionData();

        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS, data)
                 : returnInfo(API.RETURN_FAILED);
    }

    //获取老师信息
    async getTeachers (ctx, next) {
        const data = await getTeacherData();

        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS, data)
                 : returnInfo(API.RETURN_FAILED);
    }

    //选择是否为明星老师
    async changeTeacherStar (ctx, next) {
        const { id, isStar } = ctx.request.body;

        const result = await selectStarTeacher(id, isStar);

        if (!result) {
            ctx.body = returnInfo(API.SELECT_STAR_TEACHER_FAILED)
            return;
        } 

        ctx.body = returnInfo(API.SELECT_STAR_TEACHER_SUCCESS)
    }

    //获取学生信息
    async getStudents (ctx, next) {
        const data = await getStudentData();

        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS, data)
                 : returnInfo(API.RETURN_FAILED);
    }
}

module.exports = new Index();